package experimental;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.util.ArrayList;

public class CollinParserOnCollection {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws IOException, InterruptedException {
		
		String directoryName = "/home/pjbarrio/Dataset/ACE2003/ace_phase2/data/train/";
		
		String filesToProcess = "/home/pjbarrio/Dataset/ACE2003/ace_phase2/data/notCollinsProcessedYet";
		
//		String filesToProcess = "";
		
		File directory = new File(directoryName);
		
		File[] files = getFilesToProcess(directory,filesToProcess);
		
		ArrayList<String> err = new ArrayList<String>();
		
		Runtime rt = Runtime.getRuntime();
		
		Process pr = rt.exec(generateCommandLine());
		
		BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream())); //output of the process
		
		BufferedReader error = new BufferedReader(new InputStreamReader(pr.getErrorStream())); //error of the process
 
		BufferedWriter output = new BufferedWriter(new OutputStreamWriter(pr.getOutputStream())); //stdin of the process
		
		for (File file : files) {
			
			if (isColFile(file)){
			
				output.flush();
				
				int ex = generateCollinsParsed(file,error, output);
				
			}
		
		}

		String line;
		
		output.write("-1\n");

		output.flush();
		
		int currentFile = -1;
		
		while ((line = input.readLine()) != null){
			
			if (line.equals("0-NEW-0-FILE-0")){
				currentFile++;
				
				String toWrite = files[currentFile].getAbsolutePath().replaceAll(".col", ".cfg");
				
				System.setOut(new PrintStream(new File(toWrite)));
				
				System.err.println("Writing in " + toWrite);
				
				continue;
			}
						
//			System.err.println(line);
			
			System.out.println(line);
			
		}
		
		while ((line = error.readLine()) != null){
			
			System.err.println(line);
			
		}
		
		int exitVal = pr.waitFor();
		
		input.close();

		error.close();
		
		output.close();

		System.err.println("Exited with error code " + exitVal);
		
	}

	private static File[] getFilesToProcess(File directory,
			String filesToProcess) throws IOException {
		
		ArrayList<File> ret = new ArrayList<File>();
		
		if (filesToProcess.trim().equals("")){
			return directory.listFiles();
		}
		
		BufferedReader br = new BufferedReader(new FileReader(new File(filesToProcess)));
		
		String line = br.readLine();
		
		while (line!=null){
			
			ret.add(new File(directory.getAbsolutePath() + File.separator + line));
						
			line = br.readLine();
			
		}
		
		br.close();
		
		return ret.toArray(new File[0]);
	}

	private static int generateCollinsParsed(File file, BufferedReader error, BufferedWriter output) {
		
		System.err.println("Processing: " + file.getName());
		
//		Runtime rt = Runtime.getRuntime();
		
//		try {

//			Process pr = rt.exec(generateCommandLine());
//			
//			BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
//			
//			BufferedReader error = new BufferedReader(new InputStreamReader(pr.getErrorStream()));

		try {
		
			output.write("-2\n");
			
//			String line = null;
			
//			System.setOut(new PrintStream(new File(file.getAbsolutePath().replaceAll(".col", ".cfg"))));
			
			//call to process lines
			
			BufferedReader br = new BufferedReader(new FileReader(file));
			
			String sentence = br.readLine();
			
			while (sentence != null){
				
				output.write(sentence + "\n");
				
				output.flush();
				
				sentence = br.readLine();
				
			}
			
			br.close();
			
//			while ((line = input.readLine()) != null){
//				
//				System.err.println(line);
//				
//				System.out.println(line);
//				
//			}
			
			System.err.println("leaving...");
			
//			System.setOut(System.out);
			
//			int exitVal = pr.waitFor();
//			
//			input.close();

//			while ((line = error.readLine()) != null){
//				
//				System.err.println(line);
//				
//			}
			
//			error.close();
			
//			System.err.println(file.getName() + "Exited with error code " + exitVal);
			
//			return exitVal;
			
			return 1;
			
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

			
		return 0;
	}

	private static String generateCommandLine() {
		
		return new String("/home/pjbarrio/workspace/collinsParser/parser /home/pjbarrio/Software/COLLINS-PARSER/models/model1/events /home/pjbarrio/Software/COLLINS-PARSER/models/model1/grammar 10000 1 1 1 1");
		
	}

	private static boolean isColFile(File file) {
		
		return file.getName().endsWith(".col");
	}

}
